package com.learn.springcloud.web.service;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.learn.dubbo.core.util.JsonUtil;
import com.learn.dubbo.entity.user.SysUser;
import com.learn.springcloud.web.RestfulUtil;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;

@Service
public class UserService {
	
	@Autowired
	public RestfulUtil restfulUtil;
	
	public final String SERVICE_NAME="cloud-user-server"; 
	
	@HystrixCommand(fallbackMethod = "fallbackSearchAll")
	public String getSysUser(Long id) {
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("id", 1L);
		map.put("method", "getUserInfo");
		map.put("server", SERVICE_NAME);
		String result = restfulUtil.getResult(map);
		return result;
	}  
	
	
	public String index() {
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("method", "");
		map.put("server", SERVICE_NAME);
		String result = restfulUtil.getResult(map);
		return result;
	}   

	private String fallbackSearchAll(Long id) {
          System.out.println("HystrixCommand fallbackMethod handle!");
          SysUser user = new SysUser();
          user.setUserName("TestHystrix");
          user.setId(id);
          return JsonUtil.writeValueAsString(user);
     }

}
